package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;

import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);
        //数据库对Username字段做了唯一约束
        //如果没查询到
        if (emp == null) {
            return R.error("登录失败");
        }
        //密码比对失败
        if (!emp.getPassword().equals(password)) {
            return R.error("登录失败");
        }
        //状态禁用
        if (emp.getStatus() == 0) {
            return R.error("禁用");
        }
        //成功,员工id存入session
        request.getSession().setAttribute("employee", emp.getId());//数据存入session 有30分钟cd

        return R.success(emp);


    }


    @PostMapping
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("/employee");
        return R.success("退出成功");
    }

    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {

        employee.setPassword(DigestUtils.md5DigestAsHex("1we23123123".getBytes()));
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);
        employeeService.save(employee);
        return R.success("成功");

    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        log.info("page = {},pageSize = {},name = {}", page, pageSize, name);
        //分页构造器
        Page pageInfo = new Page(page, pageSize);

        //条件构造器
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();

        //过滤条件
        qw.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        //执行查询
        qw.orderByDesc(Employee::getUpdateTime);

        employeeService.page(pageInfo, qw);


        return R.success(pageInfo);
    }

    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee) {
        log.info(employee.toString());
        Long emp = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(emp);
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    @GetMapping("{id}")
    public R<Employee> getById(@PathVariable Long id) {// id在路径上
        Employee id1 = employeeService.getById(id);
        if (id1 != null) {
            return R.success(id1);
        }
        return R.success(id1);
    }
}
